package com.tsubaki.controller;


import com.alibaba.fastjson.JSON;
import com.tsubaki.entity.School;
import com.tsubaki.entity.Term;
import com.tsubaki.response.Result;
import com.tsubaki.service.SchoolService;
import com.tsubaki.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tsubaki
 * @since 2022-01-17
 */
@RestController
@RequestMapping("/school")
public class SchoolController {
    @Autowired
    private SchoolService schoolService;

    @ApiOperation("获取所有学院信息")
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    public String getAllSchool(@Nullable String pageNum, @Nullable String pageSize) {
        Integer first = StringUtil.changeString(pageNum);
        Integer second = StringUtil.changeString(pageSize);
        Map<String, Object> map = schoolService.getAll(first,second);
        if ((long)(map.get("total")) == 0){
            return JSON.toJSONString(new Result().setCode(200).setMessage("当前无学院信息"));
        }

        return JSON.toJSONString(new Result().setCode(200).setData(map));
    }

    @ApiOperation("增加学院信息")
//    @RequiresPermissions("school:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String saveSchool(@RequestBody School school) {
        schoolService.saveOrUpdate(school);
        return JSON.toJSONString(new Result().setCode(200).setMessage("添加成功"));
    }

    @ApiOperation("修改学院信息")
    @RequiresPermissions("school:update")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public String setThisTerm(@RequestBody School school) {
        schoolService.saveOrUpdate(school);
        return JSON.toJSONString(new Result().setCode(200).setMessage("修改成功"));
    }

    @ApiOperation("删除学院")
//    @RequiresPermissions("school:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public String setThisTerm(Integer schoolId) {
//        schoolService.removeById(schoolId);
        if (schoolService.removeById(schoolId)){
//            System.out.println();
            return JSON.toJSONString(new Result().setCode(200).setMessage("删除成功"));
        }else {
            return JSON.toJSONString(new Result().setCode(500).setMessage("删除失败"));
        }
    }

}
